#include "MessageDescriptor.h"
#include "ProtoFieldDescriptor.h"
#include "Message.h"

namespace miniproto
{

MessageDescriptor::MessageDescriptor()
{
	this->field_desc_list_ = new ProtoFieldDescriptorList();
}
MessageDescriptor::~MessageDescriptor()
{
	if (this->field_desc_list_ != NULL)
	{
		delete this->field_desc_list_;
		this->field_desc_list_ = NULL;
	}
}

const ::std::string &MessageDescriptor::GetTypeName() const
{
	return this->type_name_;
}

void MessageDescriptor::resize(const int size)
{
	this->field_desc_list_->resize(size);
}

void MessageDescriptor::set_field_desc(const uint8_t field_type, const ::std::string &field_name, const uint32_t number)
{
	int index = number - 1;
	if (0 <= index && index < int(this->field_desc_list_->size()))
	{
		ProtoFieldDescriptor &field_desc = this->field_desc_list_->at(index);
		field_desc.set(field_type, field_name, number);
	}
}

void MessageDescriptor::set_message_field_desc(const uint8_t field_type, const ::std::string &field_name, const uint32_t number, ::miniproto::Message *message)
{
	int index = number - 1;
	if (0 <= index && index < int(this->field_desc_list_->size()))
	{
		ProtoFieldDescriptor &field_desc = this->field_desc_list_->at(index);
		field_desc.set_message(field_type, field_name, number, message);
	}
}

const ::miniproto::ProtoFieldDescriptor *MessageDescriptor::get_field_desc(const int number) const
{
	int index = number - 1;
	if (0 <= index && index < int(this->field_desc_list_->size()))
	{
		return &(this->field_desc_list_->at(index));
	}
	return NULL;
}

}
